﻿#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDebug>
#include <QFileInfo>
#include <QFile>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setWindowTitle("CalcTreeSpace");
    ui->treeWidget->clear();
    ui->treeWidget->setColumnCount(0);

    //test
    ui->lineEdit->setText("D:\\chenchao\\share\\SmallGUI\\base\\src");
    ui->lineEdit_2->setText("0");
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_pushButton_clicked()
{
    QString dirpath = ui->lineEdit->text();
    int level = ui->lineEdit_2->text().toInt();
    QString excludeFlag = ui->lineEdit_4->text();

    m_excludeFlags.clear();

    if(excludeFlag.contains("|")){
        m_excludeFlags = excludeFlag.split("|");
    }
    else{
        m_excludeFlags.append(excludeFlag);
    }

    if(excludeFlag.trimmed().isEmpty()){
        m_excludeFlags.clear();
    }

    if(dirpath.trimmed().isEmpty()){
        return;
    }

    if(level > 20) {
        return;
    }

    if(level < 0) level = 0;

    m_maxLevel = level;

    if(dirpath.contains("\\")){
        qDebug()<<"dirpath.contains(\\)" << dirpath;
        dirpath = dirpath.replace("\\", "/");
        qDebug()<<"change="<<dirpath;
    }

    QFileInfo info(dirpath);
    if(info.isFile()){
        return;
    }
    qDebug()<<"is dir";

    QDir dir(dirpath);
    uint64_t temp=0;
    Calc(dir, m_info, temp);

    //show tree
    ShowTreeWidget();
}

QString MainWindow::MemNumToStr(uint64_t v)
{
    uint64_t one = 1024;
    if(v < one){
        return QString::number(v) + "B";
    }
    else if(v < one * one){
        return QString::number(v/one) + "KB";
    }
    else if(v < one * one * one){
        return QString::number(v/(one*one)) + "MB";
    }
    else if(v < one * one * one * one){
        return QString::number(v/(one * one * one)) + "GB";
    }
    else if(v < one * one * one * one * one){
        return QString::number(v/(one * one * one * one)) + "TB";
    }
    return "";
}

void MainWindow::Calc(const QDir &dir, MyInfo& info, uint64_t& size)
{

    QStringList filters;
    filters<<"*";

    QFileInfoList list = dir.entryInfoList(filters,QDir::NoDotAndDotDot | QDir::AllEntries,QDir::NoSort);

    info.name = dir.dirName();
    info.childes.clear();

    uint64_t child_size = 0;

    for(int i=0;i<list.length();i++){

        MyInfo info_child;
        QFileInfo fileInfo = list.at(i);

        if(fileInfo.isDir()){
            //目录
            bool exist = false;
            foreach(QString a, m_excludeFlags){
                if(a.trimmed().isEmpty()) continue;
                if(fileInfo.filePath().endsWith(a.trimmed())){
                    exist = true;
                    break;
                }
            }
            if(exist){
                continue;
            }
            //qDebug()<<"fileInfo.filePath() " << fileInfo.filePath();
            QDir tempDir(fileInfo.filePath());
            Calc(tempDir, info_child, child_size);
        }
        else{
            //文件
            info_child.name = fileInfo.fileName();
            info_child.size = CalcSize(fileInfo.filePath());

            child_size += info_child.size;
        }
        info.childes.append(info_child);
    }

    size += child_size;

    info.size = child_size;
}

uint64_t MainWindow::CalcSize(const QString path)
{
    uint64_t size = 0;

    QFile file(path);

    size = file.size();

    return size;
}

void MainWindow::ShowTreeWidget()
{
    ui->treeWidget->clear();

    QStringList heads;
    heads<<"name"<<"size";
    ui->treeWidget->setHeaderLabels(heads);
    ui->treeWidget->setColumnCount(2);
    ui->treeWidget->setColumnWidth(0, 200);

    QTreeWidgetItem* item = new QTreeWidgetItem();
    item->setText(0, m_info.name);
    item->setText(1, MemNumToStr(m_info.size));

    ui->treeWidget->addTopLevelItem(item);

    ShowTreeWidgetRecursion(item, m_info, 0);

    item->setExpanded(true);

}

void MainWindow::ShowTreeWidgetRecursion(QTreeWidgetItem* parent, MyInfo &info, int level)
{
    //qDebug()<<"ShowTreeWidgetRecursion::: " << level << info.name;
    if(level >= m_maxLevel) {
        return;
    }

    for(int i =0;i<info.childes.size();i++){
         QTreeWidgetItem* item = new QTreeWidgetItem(parent);
         item->setText(0, info.childes[i].name);

         QString v = MemNumToStr(info.childes[i].size);
         item->setText(1, v);

         if(v.contains("GB")){
             item->setBackgroundColor(1, Qt::red);
         }
         else if(v.contains("MB")){
             item->setBackgroundColor(1, Qt::yellow);
         }

         //qDebug()<<"ShowTreeWidgetRecursion:" << level << info.childes[i].name << info.childes[i].childes.size();
         if(info.childes[i].childes.size() > 0){
             ShowTreeWidgetRecursion(item, info.childes[i], level + 1);
         }

         item->setExpanded(true);
    }
}


void MainWindow::on_lineEdit_2_textChanged(const QString &arg1)
{
    on_pushButton_clicked();
}


void MainWindow::on_lineEdit_4_textChanged(const QString &arg1)
{
    on_pushButton_clicked();
}

